package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZhcaMyreportAddBo;
import com.ruoyi.system.bo.ZhcaMyreportQueryBo;
import com.ruoyi.system.bo.ZhcaMyreportEditBo;
import com.ruoyi.system.domain.ZhcaMyreport;
import com.ruoyi.system.mapper.ZhcaMyreportMapper;
import com.ruoyi.system.vo.ZhcaMyreportVo;
import com.ruoyi.system.service.IZhcaMyreportService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 上报管理Service业务层处理
 *
 * @author huaqihao
 * @date 2021-07-31
 */
@Service
public class ZhcaMyreportServiceImpl extends ServicePlusImpl<ZhcaMyreportMapper, ZhcaMyreport> implements IZhcaMyreportService {

    @Override
    public ZhcaMyreportVo queryById(Long id){
        return getVoById(id, ZhcaMyreportVo.class);
    }

    @Override
    public TableDataInfo<ZhcaMyreportVo> queryPageList(ZhcaMyreportQueryBo bo) {
        PagePlus<ZhcaMyreport, ZhcaMyreportVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ZhcaMyreportVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZhcaMyreportVo> queryList(ZhcaMyreportQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZhcaMyreportVo.class);
    }

    private LambdaQueryWrapper<ZhcaMyreport> buildQueryWrapper(ZhcaMyreportQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZhcaMyreport> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getTitle()), ZhcaMyreport::getTitle, bo.getTitle());
        lqw.eq(StrUtil.isNotBlank(bo.getContent()), ZhcaMyreport::getContent, bo.getContent());
        lqw.eq(bo.getType() != null, ZhcaMyreport::getType, bo.getType());
        lqw.eq(bo.getState() != null, ZhcaMyreport::getState, bo.getState());
        lqw.eq(bo.getUserid() != null, ZhcaMyreport::getUserid, bo.getUserid());
        lqw.eq(StrUtil.isNotBlank(bo.getPicurl()), ZhcaMyreport::getPicurl, bo.getPicurl());
        lqw.eq(StrUtil.isNotBlank(bo.getAddress()), ZhcaMyreport::getAddress, bo.getAddress());
        lqw.eq(bo.getTime() != null, ZhcaMyreport::getTime, bo.getTime());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZhcaMyreportAddBo bo) {
        ZhcaMyreport add = BeanUtil.toBean(bo, ZhcaMyreport.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZhcaMyreportEditBo bo) {
        ZhcaMyreport update = BeanUtil.toBean(bo, ZhcaMyreport.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZhcaMyreport entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
